import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login1.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/Home.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: 'welcome',
        component: () => import('../views/Welcome.vue')
      },
      {
        path: '/users',
        name: 'users',
        component: () => import('../components/user/Users.vue')
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import('../components/rights/Rights.vue')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('../components/rights/Roles.vue')
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import('../components/goods/categories.vue')
      },
      {
        path: '/params',
        name: 'params',
        component: () => import('../components/goods/params.vue')
      },
      {
        path: '/goods',
        name: 'params',
        component: () => import('../components/goods/goods.vue')
      },
      {
        path: '/goods/add',
        name: 'goods_add',
        component: () => import('../components/goods/add.vue')
      },
      {
        path: '/goods/edit',
        name: 'goods_edit',
        component: () => import('../components/goods/edit.vue')
      },
      {
        path: '/orders',
        name: 'orders',
        component: () => import('../components/order/Order.vue')
      },
      {
        path: '/reports',
        name: 'report',
        component: () => import('../views/Welcome.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next()放行     next('/login')强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
